const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const {
  logout
} = require('../../apiConfig/apiList/luban');
const {
  listByLoginUser,
  listBySign,
} = require('../../apiConfig/apiList/luban')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    orderList: [],
    pageNum: 1,
    pageSize: 10,
    isEnd: false,
    sumRepairedPrice: '0.00',
    orderInfoList: [
      {
        num: 10,
        name: '待上门报价',
      },
      {
        num: 1,
        name: '已报价待确认',
      },
      {
        num: 1,
        name: '服务中待验收',
      },
      {
        num: 1,
        name: '已取消',
      },
      {
        num: 19,
        name: '已验收待结算',
      },
      {
        num: 190,
        name: '交易成功',
      },
    ],
    dateStart: '2016-09-01',
    dateEnd: '2016-09-02',
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    schoolChecked: '',
    showSchool: false,
    schoolList: [
      {
        name: '选项1',
        value: '1',
      },
      {
        name: '选项232',
        value: '2',
      },
    ],
    statusChecked: '',
    showStatus: false,
    statusList: [
      {
        name: '待报价',
        value: '1',
      },
      {
        name: '已接单',
        value: '2',
      },
      {
        name: '已报价',
        value: '3',
      },
      {
        name: '同意报价',
        value: '4',
      },
      {
        name: '待验收',
        value: '5',
      },
      {
        name: '验收通过',
        value: '6',
      },
      {
        name: '已完成',
        value: '7',
      },
      {
        name: '已关闭',
        value: '-1',
      },
      {
        name: '不同意报价',
        value: '-2',
      },
      {
        name: '验收不通过',
        value: '-3',
      },
    ],
    params: {
      keyword: '',
      status: '',
      startTime: '',
      endTIme: '',
      pageNum: 1,
      pageSize: 10,
      orderByColumn: 'create_time',
      isAsc: 'desc',
    }
  },

  onShow() {
    this.getOrderList();
    this.getSchool();
  },
  onLogout(){
    logout().then(res=>{
      if(res.code == 200){
        console.log('退出登录')
        wx.redirectTo({
          url: '/pages/authLogin/index',
        })
      }
    })
  },
  onSearch(){
    let params = this.data.params;
    listByLoginUser(params).then(res=>{
      if(res.code == 200){
        let data = res.rows;
        let sum = res.tatalPrice;
        this.setData({
          orderList: data,
          sumRepairedPrice:sum
        });
      }
    })
  },
  getSchool(){
    let params = {
      pageNum: 1,
      pageSize: 100,
      orderByColumn: 'create_time',
      isAsc: 'desc',
    };
    listBySign(params).then(res=>{
      if(res.code == 200){
        let data = res.rows;
        let temp = [];
        data.forEach(item=>{
          temp.push({
            name: item.schoolName,
            value: item.id,
            id: item.id,
            phone: item.phone,
          })
        })
        this.setData({
          schoolList: temp
        });
      }
    })
  },
  pullOrderList(){
    if(!this.data.isEnd) {
      this.getOrderList();
    }else{
      wx.showToast({
        title: '已加载全部',
        icon: 'none',
        duration: 1000,
      })
    }
  },
  getOrderList(){
    if(this.data.isEnd)return;
    let that = this;
    let list = this.data.orderList;
    let params = this.data.params;
    listByLoginUser(params).then(res=>{
      if(res.code == 200){
        let data = res.rows;
        this.setData({
          orderList: list.concat(data),
          sumRepairedPrice:res.tatalPrice
        });
        if(that.data.orderList.length < res.total){
          params.pageNum++;
          console.log('params.pageNum', params.pageNum)
        }else{
          this.setData({
            isEnd: 'ture'
          });
        }
      }
    })
  },
  showSchoolEevent: function(){
    this.setData({ showSchool: true });
    console.log('showSchoolEevent', this.data.showSchool)
  },
  schoolCSelect(e) {
    this.setData({
      'params.schoolId': e.detail.id,
      schoolChecked: e.detail.name
    })
    this.setData({ showSchool: false });
  },
  schoolCancel() {
    this.setData({ showSchool: false });
  },
  schoolClose() {
    this.setData({ showSchool: false });
  },
  showStatusEevent: function(){
    this.setData({ showStatus: true });
    console.log('showSchoolEevent', this.data.showSchool)
  },
  statusSelect(e) {
    this.setData({
      'params.status': e.detail.value,
      statusChecked: e.detail.name
    })
    this.setData({ showStatus: false });
  },
  statusClose() {
    this.setData({ showStatus: false });
  },

  bindDateStart: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'params.startTime': e.detail.value
    })
  },
  bindDateEnd: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'params.endTIme': e.detail.value
    })
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      "userInfo.nickName": nickName,
    })
    console.log(3423423, nickName)
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },

  /**
   * 前往我签约的学校
   */
  toMySignSchool(){
    wx.navigateTo({
      url: '/pages/service-signSchool/index'
    });
  },
  toMyInfo(){
    wx.navigateTo({
      url: '/pages/service-info/index'
    });
  },
  toFeeStandard(){
    wx.navigateTo({
      url: '/pages/fee-standard/index'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})